<?php


namespace app\common\service\rabbitMQ;


use app\common\enum\MainEnum;
use app\common\exception\LogicException;
use app\common\model\OrderModel;
use PhpAmqpLib\Exchange\AMQPExchangeType;
use PhpAmqpLib\Message\AMQPMessage;
use think\facade\Db;

class UserCreated implements RabbitMQBusinessInterface
{
    public static function instance($expire_timestamp = 0): RabbitMQ
    {
        $rabbitMQ = (new RabbitMQ());
        $rabbitMQ->setExchange(__CLASS__.'_direct');
        $rabbitMQ->setExchangeType(AMQPExchangeType::DIRECT);
        if ($expire_timestamp) {
            $rabbitMQ->setXDelay($expire_timestamp);
        }
        $rabbitMQ->setRoutingKey(__CLASS__);
        $rabbitMQ->setQueue(__CLASS__);
        $rabbitMQ->setConfirmSelectType(
            3,
            [
                static function (AMQPMessage $message) {
                },
                static function (AMQPMessage $message) {
                    throw new LogicException();
                },
                static function ($replyCode, $replyText, $exchange, $routingKey, AMQPMessage $message) {
                },
            ]
        );
        $rabbitMQ->prepare();
        $rabbitMQ->preparePublisher();
        return $rabbitMQ;
    }

    public static function publisher(array $data, int $expire_timestamp = 0): void
    {
        $rabbitMQ = self::instance($expire_timestamp);
        $rabbitMQ->publisher(
            [
                [
                    'data' => $data,
                ],
            ]
        );
    }

    public static function consumer($body): bool
    {
        Db::startTrans();
        try {
//            Db::commit();
            return true;
        } catch (\Exception $e) {
            Db::rollback();
            return false;
        }
    }
}